﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace LowLevelGraphics.Filter
{   
    /// <summary>
    ///Removes isolated pixels (individual 1s that are surrounded by 0s), such as the center pixel in this pattern.
    ///0  0  0 
    ///0  1  0 
    ///0  0  0
    /// </summary>
    /// <remarks>See http://www.johnloomis.org/ece563/notes/BinaryImages/morph/morph.html to see what result we should get</remarks>
    public class Clean : BaseForeBackgroundColorFilter
    {
        //TODO: check if this can be deleted (because of inheritance now)
        //protected Dictionary<NEIGHBOUR, List<Point>> m_aNeighbour = new Dictionary<NEIGHBOUR, List<Point>>() { 
        //    {NEIGHBOUR.EIGHT, Definitions.EnvironmentPointListClockwise},
        //    {NEIGHBOUR.FOUR, Definitions.EnvironmentPointList4Clockwise}
        //};

        //TODO: check if this can be deleted (inheritance)
        protected NEIGHBOUR m_NEIGHBOUR = NEIGHBOUR.EIGHT;

        /// <summary>
        /// Initializes a new instance of the <see cref="Clean"/> class.
        /// </summary>
        public Clean()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Clean"/> class.
        /// </summary>
        /// <param name="_clean">The _clean.</param>
        internal Clean(Clean _clean)
        {
        }

        //TODO: check if this could be deleted (inheritance)
        /// <summary>
        /// Gets or sets the neighbour.
        /// </summary>
        /// <value>The neighbour.</value>
        public NEIGHBOUR Neighbour
        {
            get { return m_NEIGHBOUR; }
            set { m_NEIGHBOUR = value; }
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            int y = 0;
            int x = 0;
            int nHeight = _bitmap.Height;
            int nWidth = _bitmap.Width;

            int nNeighbourCount = m_aNeighbour[m_NEIGHBOUR].Count;
            Color[] aColor = new Color[nNeighbourCount];

            int nMax = 7;
            UnsafeBitmap bitmapCloned = (UnsafeBitmap)_bitmap.Clone();
            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    Color color = _bitmap.GetPixel(x, y);

                    if (color == m_ColorBackground) continue;

                    aColor = Segmentation.GetEnvironmentPixelColorArray(bitmapCloned, x, y, m_aNeighbour[m_NEIGHBOUR], aColor);

                    int nBackgroundCount = 0;

                    foreach (Color colorEnvironment in aColor)
                    {
                        if (colorEnvironment == m_ColorBackground)
                        {
                            nBackgroundCount++;
                        }
                    }
                    if (nBackgroundCount == nNeighbourCount)
                    {
                        _bitmap.SetPixel(x, y, m_ColorBackground);
                    }
                }
            }
            bitmapCloned.Dispose();
            return _bitmap;
        }

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new Clean(this);
        }
    }
}
